﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using SYcwpb.Models;

namespace SYcwpb.Helpers
{
    public class CONSTDATA
    {
        public const string IP = "127.0.0.1"; // 默认远程ip地址

        public const int Port = 8888;

        public const int Driver_Tips_Interval = 1000 * 10 * 1;//半分钟

        public static LoginModel UserModel = LoginModel.CreateLoginModel();

        /// <summary>
        /// 迟到分等级的时间
        /// </summary>
        public static int DelayInter = 15;

        private static Dictionary<string, int> SationName_To_ID;

        private static Dictionary<int, string> StationID_To_Name;

        public static Dictionary<int, string> initStationID_To_Name()
        {
            StationID_To_Name = new Dictionary<int, string>();

            StationID_To_Name.Add(0, "北客折返轨");

            StationID_To_Name.Add(1, "北客站");
            StationID_To_Name.Add(2, "北苑站");
            StationID_To_Name.Add(3, "运动公园站");
            StationID_To_Name.Add(4, "行政中心站");
            StationID_To_Name.Add(5, "凤城五路站");
            StationID_To_Name.Add(6, "市图书馆站");
            StationID_To_Name.Add(7, "大明宫西站");
            StationID_To_Name.Add(8, "龙首原站");
            StationID_To_Name.Add(9, "安远门站");
            StationID_To_Name.Add(10, "北大街站");
            StationID_To_Name.Add(11, "钟楼站");
            StationID_To_Name.Add(12, "永宁门站");
            StationID_To_Name.Add(13, "南稍门站");
            StationID_To_Name.Add(14, "体育场站");
            StationID_To_Name.Add(15, "小寨站");
            StationID_To_Name.Add(16, "纬一街站");
            StationID_To_Name.Add(17, "会展中心站");

            StationID_To_Name.Add(18, "会展折返轨");

            return StationID_To_Name;
        }

        public static Dictionary<string,int>InitSationName_To_ID_1()
        {
            SationName_To_ID = new Dictionary<string, int>();

            SationName_To_ID.Add("SSH",1);
            SationName_To_ID.Add("ZYD",2);
            SationName_To_ID.Add("QHJ",3);
            SationName_To_ID.Add("SHJ",4);
            SationName_To_ID.Add("ZSZ",5);
            SationName_To_ID.Add("KFD",6);
            SationName_To_ID.Add("YHG",7);
            SationName_To_ID.Add("YBL",8);
            SationName_To_ID.Add("ZGJ",9);
            SationName_To_ID.Add("QGJ",10);
            SationName_To_ID.Add("BGJ",11);
            SationName_To_ID.Add("TXG",12);
            SationName_To_ID.Add("YFB",13);
            SationName_To_ID.Add("SYZ",14);
            SationName_To_ID.Add("TYJ",15);
            SationName_To_ID.Add("NSC",16);
            SationName_To_ID.Add("QND",17);
            SationName_To_ID.Add("HYM", 18);
            SationName_To_ID.Add("ZJZ", 19);
            SationName_To_ID.Add("DZJ", 20);
            SationName_To_ID.Add("PJJ", 21);
            SationName_To_ID.Add("LMG", 22);

            SationName_To_ID.Add("TLM",30);
            SationName_To_ID.Add("TSH",31);

            SationName_To_ID.Add("WD1",66);
            SationName_To_ID.Add("WD2",67);

            return SationName_To_ID;
        }

        public static Dictionary<string, int> InitSationName_To_ID_2()
        {
            SationName_To_ID = new Dictionary<string, int>();

            SationName_To_ID.Add("QYL", 1);
            SationName_To_ID.Add("BTH", 2);
            SationName_To_ID.Add("SJD", 3);
            SationName_To_ID.Add("YPJ", 4);
            SationName_To_ID.Add("ATZ", 5);
            SationName_To_ID.Add("WLH", 6);
            SationName_To_ID.Add("STS", 7);
            SationName_To_ID.Add("GYZ", 8);
            SationName_To_ID.Add("QNG", 9);
            SationName_To_ID.Add("QND", 10);
            SationName_To_ID.Add("SFG", 11);
            SationName_To_ID.Add("JRZ", 12);
            SationName_To_ID.Add("SYB", 13);
            SationName_To_ID.Add("QSL", 14);
            SationName_To_ID.Add("ZYY", 15);
            SationName_To_ID.Add("BLG", 16);
            SationName_To_ID.Add("XLY", 17);
            SationName_To_ID.Add("LXZ", 18);
            SationName_To_ID.Add("STZ", 19);

            SationName_To_ID.Add("TST", 30);
            SationName_To_ID.Add("TSG", 31);

            SationName_To_ID.Add("ED1", 66);
            SationName_To_ID.Add("ED2", 67);

            return SationName_To_ID;
        }

        /*-----------------------------判断文件是否打开------------------------------------*/
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);

        public static int OF_READWRITE = 2;
        public static int OF_SHARE_DENY_NONE = 0x40;
        public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);

    }

    public static class GlobalVariables
    {
        public static bool IsNumberic(string _string)
        {
            int a = 0;
            return int.TryParse(_string, out a);
        }

        public static void GetLineTwoTrain(string tbname, ref ArrayList TrainIdofTwo)
        {
            string sql = "select distinct MidID from [" + tbname + "] order by MidID ASC";
            try
            {
                TrainIdofTwo.Clear();
                SqlDataLayer traindivide = new SqlDataLayer();
                DataTable dtLineTwo = traindivide.SelectDt(sql);
                if (dtLineTwo != null)
                {
                    for (int i = 0; i < dtLineTwo.Rows.Count; i++)
                    {
                        TrainIdofTwo.Add(dtLineTwo.Rows[i]["MidID"].ToString());
                    }
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        public static string LoadHeadImage(string user_en)
        {
            //string fold = "HeadImages";
            string imageRoot = System.Windows.Forms.Application.StartupPath;

            string imagePath = imageRoot + @"\headImages\" + user_en + ".jpg";
            string defaultPath = imageRoot + @"\Images\defaultHeadImage.jpg";

            if (!Directory.Exists(imageRoot + @"\headImages\"))
            {
                Directory.CreateDirectory(imageRoot + @"\headImages\");
            }

            if (!File.Exists(imagePath))
                if (!File.Exists(defaultPath))
                    return null;
                else
                    return defaultPath;

            return imagePath;
        }

        public static StringBuilder DecryptCode(string code)
        {
            MD5 md5pwd = MD5.Create();
            byte[] criptdata = md5pwd.ComputeHash(Encoding.Default.GetBytes(code));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < criptdata.Length; i++)
            {
                sb.Append(criptdata[i].ToString("x2"));
            }
            return sb;
        }

        /// <summary>
        /// 将秒以hh:mm:ss的形式表示
        /// </summary>
        /// <param name="timestr"></param>
        /// <returns></returns>
        public static string FormatTime(string timestr)
        {
            if (timestr.Length < 1) { return null; }
           
            int getobj = Convert.ToInt32(timestr);
            if (getobj > 86400)
            {
                getobj -= 86400;
            }
            long a, b, c;
            string ret = string.Empty;
            if (getobj > 0)
            {
                a = (getobj / 3600);
                b = ((getobj % 3600) / 60);
                c = ((getobj % 3600) % 60);
                ret = a < 10 ? "0" + a.ToString() : a.ToString();
                ret += ":";
                ret += b < 10 ? "0" + b.ToString() : b.ToString();
                ret += ":";
                ret += c < 10 ? "0" + c.ToString() : c.ToString();
            }
            return ret;
        }

        /// <summary>
        /// 将hh:mm:ss的形式以秒返回
        /// </summary>
        /// <param name="strTime"></param>
        /// <returns></returns>
        public static int FormatTimeToInt(string strTime)
        {
            if (strTime == null || strTime.Length < 1) { return 0; }
            string[] timeArr = strTime.Split(':');
            int hour = Convert.ToInt32(timeArr[0]);
            int mm = Convert.ToInt32(timeArr[1]);
            int ss = Convert.ToInt32(timeArr[2]);

            return hour * 3600 + mm * 60 + ss;
        }

        /// <summary>
        /// 获取某个车次的发车时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetStartTime(string tbName,string trainId,int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select top 1 Depart from [" + tbName + "] where TrainId = '" + trainId + "' and Depart<>-1 order by Depart asc";
            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlStart);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlStart))); }
            
        }

        /// <summary>
        /// 获取某车次于某站的到站时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetArriveTimeFromSta(string tbName, string trainId,int StaID, int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select Arrive from [" + tbName + "] where TrainId = '" + trainId + "' and Station = " + StaID + " and Arrive<>0";
            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlStart);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlStart))); }

        }

        /// <summary>
        /// 获取某车次于某站的发车时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetDepartTimeFromSta(string tbName, string trainId, int StaID, int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select Depart from [" + tbName + "] where TrainId = '" + trainId + "' and Station = " + StaID + " and Depart<>-1";
            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlStart);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlStart))); }

        }


        /// <summary>
        /// 返回提前后的时间
        /// </summary>
        /// <param name="myTime">HH:mm:ss</param>
        /// <param name="minAhead">int min</param>
        /// <returns></returns>
        public static string GetAheadTime(string myTime,int minAhead)
        {
            string result = (FormatTimeToInt(myTime) - minAhead * 60).ToString();
            return GlobalVariables.FormatTime(result);
        }

        /// <summary>
        /// 获取某个车次的结束时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="trainId">车次号</param>
        /// <returns></returns>
        public static object GetFinishTime(string tbName, string trainId,int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlFinish = "select top 1 Arrive from [" + tbName + "] where TrainId = '" + trainId + "' order by Arrive desc";

            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlFinish);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlFinish))); }
            

        }

    }

}
